package com.itqf.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.entity.SysMenu;
import com.itqf.entity.SysMenuExample;
import com.itqf.entity.SysUser;
import com.itqf.mapper.SysMenuMapper;
import com.itqf.mapper.SysUserMapper;
import com.itqf.service.SysMenuService;
import com.itqf.utils.R;
import com.itqf.utils.ResultData;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 徐老板
 * @date 2020/1/25   21:58
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private SysUserMapper sysUserMapper;

//    @Override
//    public ResultData findByPage(int limit, int offset) {
//        PageHelper.offsetPage(offset,limit );
//        List<SysMenu> menuList = sysMenuMapper.selectByExample(null);
//
//        PageInfo pageInfo = new PageInfo(menuList);
//        long total = pageInfo.getTotal();
//        List list = pageInfo.getList();
//        ResultData resultData=new ResultData(total,list );
//        return resultData;
//    }

    @Override
    public ResultData findByPage(int limit, int offset, String search,String sort,String order) {
        PageHelper.offsetPage(offset,limit );
        SysMenuExample sysMenuExample =null;

        sysMenuExample= new SysMenuExample();
        SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();

        if (search!=null&&!"".equals(search)){

           criteria.andNameLike("%"+search+"%");
        }

        if (sort!=null&&sort.equals("menuId")){
            sort="menu_id";
        }

        sysMenuExample.setOrderByClause(sort+" "+order);

        List<SysMenu> sysMenuList = sysMenuMapper.selectByExample(sysMenuExample);

        PageInfo pageInfo=new PageInfo(sysMenuList);
        long total = pageInfo.getTotal();
        List rows = pageInfo.getList();
        System.out.println("service层总数是"+total+"数据是"+rows);
        ResultData resultData=new ResultData(total,rows );
        return resultData;
    }

//    删除操作
    @Override
    public R delList(List<Long> ids) {

        SysMenuExample sysMenuExample =new SysMenuExample();
        SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();
        SysMenuExample.Criteria menuIdIn = criteria.andMenuIdIn(ids);
        if (menuIdIn==null||"".equals(menuIdIn)){
            return R.error("请勾选需要删除的数据");
        }
        for (Long id : ids) {
            if (id<31){
                return R.error("此数据需要权限删除");
            }
        }
        int i = sysMenuMapper.deleteByExample(sysMenuExample);
        if (i!=0){
            return R.ok("删除成功");
        }else {
            return R.error("删除失败");
        }

    }

    @Override
    public R selectMenu() {
        List<SysMenu> sysMenus = sysMenuMapper.findMenuNotButton();
        SysMenu sysMenu = new SysMenu();
        sysMenu.setMenuId(0L);
        sysMenu.setParentId(-1L);
        sysMenu.setName("自创曾祖父级目录");
        sysMenu.setOrderNum(0);


        sysMenus.add(sysMenu);

        return R.ok().put("menuList", sysMenus);
//        SysMenuExample example=new SysMenuExample();
//        SysMenuExample.Criteria criteria = example.createCriteria();
//        criteria.andTypeNotEqualTo(2);
//        List<SysMenu> list = sysMenuMapper.selectByExample(example);
//        return R.ok().put("muenList",list );
    }

    @Override
    public R menuSave(SysMenu sysMenu) {
        int insert = sysMenuMapper.insert(sysMenu);
        if (insert>0){
            return R.ok("保存成功");
        }else {
            return R.error("保存失败");
        }

    }

    @Override
    public R menuFind(long menuId) {
        SysMenu sysMenu = sysMenuMapper.selectByPrimaryKey(menuId);

        return R.ok().put("menu",sysMenu );
    }

    @Override
    public R menuUpdate(SysMenu sysMenu) {
        int i = sysMenuMapper.updateByPrimaryKey(sysMenu);
        return i>0?R.ok("恭喜你大哥修改成功"):R.error("不好意思大哥 修改失败");
    }


    /**
     *以下为手动拼接的json数据
     */



    @Override
    public R findUserMenu() {
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser) subject.getPrincipal();

        List<SysMenu> dir = sysMenuMapper.findDir(sysUser.getUserId());
//        List<SysMenu> dir = sysMenuMapper.findDir(1);
        List childList =null;
        List<String> permsList=null;
        for (SysMenu sysMenu : dir) {
           childList = sysMenuMapper.findChildList(sysMenu.getMenuId(), sysUser.getUserId());
//           permsList = sysUserMapper.findPermsByUID(sysUser.getUserId());
           sysMenu.setList(childList);
//           sysMenu.setParamList(permsList);
        }
        permsList = sysUserMapper.findPermsByUID(sysUser.getUserId());

        return R.ok().put("menuList", dir).put("permissions", permsList);
    }



}
